﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ScienceAIApp.Theme.Controls;
/// <summary>
/// Interaction logic for ToolWindowSlider.xaml
/// </summary>
public partial class ToolWindowSlider : UserControl
{
    private static int width = 300; // 拖动条初始宽度
    private static int bigWidth = 320; // 拖动条初始宽度
    private static int height = 60; // 高度
    private static int min = 0; // 最小值
    private static int max = 255; // 最大值
    private static int freq = 1; // 出现刻度的间距

    public ToolWindowSlider()
    {
        InitializeComponent();
    }

    /// <summary>
    /// 裁剪矩阵（头）
    /// </summary>
    private Rect StartRect
    {
        get { return (Rect)GetValue(StartRectProperty); }
        set { SetValue(StartRectProperty, value); }
    }

    private static readonly DependencyProperty StartRectProperty =
        DependencyProperty.Register("StartRect", typeof(Rect), typeof(ToolWindowSlider));

    /// <summary>
    /// 裁剪矩阵（尾）
    /// </summary>
    private Rect EndRect
    {
        get { return (Rect)GetValue(EndRectProperty); }
        set { SetValue(EndRectProperty, value); }
    }

    private static readonly DependencyProperty EndRectProperty =
        DependencyProperty.Register("EndRect", typeof(Rect), typeof(ToolWindowSlider));


    /// <summary>
    /// 刻度间距，默认为10
    /// </summary>
    public int SliderTickFrequency
    {
        get { return (int)GetValue(SliderTickFrequencyProperty); }
        set { SetValue(SliderTickFrequencyProperty, value); }
    }

    public static readonly DependencyProperty SliderTickFrequencyProperty =
        DependencyProperty.Register("SliderTickFrequency", typeof(int), typeof(ToolWindowSlider),
            new PropertyMetadata(freq));

    /// <summary>
    /// 控件高度，默认为60
    /// </summary>
    public int SliderHeight
    {
        get { return (int)GetValue(SliderHeightProperty); }
        set { SetValue(SliderHeightProperty, value); }
    }

    public static readonly DependencyProperty SliderHeightProperty =
        DependencyProperty.Register("SliderHeight", typeof(int), typeof(ToolWindowSlider),
            new PropertyMetadata(height));

    /// <summary>
    /// 拖动条宽度，默认为150
    /// </summary>
    public int SliderWidth
    {
        get { return (int)GetValue(SliderWidthProperty); }
        set { SetValue(SliderWidthProperty, value); }
    }

    public int BigSliderWidth
    {
        get { return (int)GetValue(SliderWidthProperty); }
        set { SetValue(SliderWidthProperty, value); }
    }

    public static readonly DependencyProperty SliderWidthProperty =
        DependencyProperty.Register("SliderWidth", typeof(int), typeof(ToolWindowSlider),
            new PropertyMetadata(width));

    public static readonly DependencyProperty BigSliderWidthProperty =
        DependencyProperty.Register("BigSliderWidth", typeof(int), typeof(ToolWindowSlider),
            new PropertyMetadata(bigWidth));

    /// <summary>
    /// 最小值，默认为0
    /// </summary>
    public int Minimum
    {
        get { return (int)GetValue(MinimumProperty); }
        set { SetValue(MinimumProperty, value); }
    }

    public static readonly DependencyProperty MinimumProperty =
        DependencyProperty.Register("Minimum", typeof(int), typeof(ToolWindowSlider), new PropertyMetadata(min));

    /// <summary>
    /// 最大值，默认为100
    /// </summary>
    public int Maximum
    {
        get { return (int)GetValue(MaximumProperty); }
        set { SetValue(MaximumProperty, value); }
    }

    public static readonly DependencyProperty MaximumProperty =
        DependencyProperty.Register("Maximum", typeof(int), typeof(ToolWindowSlider), new PropertyMetadata(max));

    /// <summary>
    /// 选中开始值，默认为0
    /// </summary>
    public int StartValue
    {
        get { return (int)GetValue(StartValueProperty); }
        set { SetValue(StartValueProperty, value); }
    }

    public static readonly DependencyProperty StartValueProperty =
        DependencyProperty.Register("StartValue", typeof(int), typeof(ToolWindowSlider), new PropertyMetadata(min));

    /// <summary>
    /// 选中结束值，默认为100
    /// </summary>
    public int EndValue
    {
        get { return (int)GetValue(EndValueProperty); }
        set { SetValue(EndValueProperty, value); }
    }

    public static readonly DependencyProperty EndValueProperty =
        DependencyProperty.Register("EndValue", typeof(int), typeof(ToolWindowSlider), new PropertyMetadata(max));

    /// <summary>
    /// 对两个拖动条进行裁剪
    /// 对裁剪方案就行了调整，但总体方案一般。对右滑块进行了单独的权重长度调整
    /// TODO：有更好的方案请取代
    /// </summary>
    private void ClipSlider()
    {
        try
        {
            int totalValue = Maximum - Minimum;
            if (totalValue == 0) return;
            double leftClip = SliderWidth * (StartValue - Minimum) / totalValue;
            double rightClip = SliderWidth * (EndValue - Minimum) / totalValue;
            // 对第一个拖动条进行裁剪
            StartRect = new Rect(0, 0, leftClip + 18, SliderHeight);
            // 对第二个拖动条进行裁剪
            if (rightClip - 18 < SliderWidth / 2)
            {
                EndRect = new Rect(rightClip - 10, 0, SliderWidth, SliderHeight);
            }
            else
            {
                EndRect = new Rect(rightClip - 18, 0, SliderWidth, SliderHeight);
            }
        }
        catch (Exception e)
        {
        }
    }

    /// <summary>
    /// 初始化裁剪
    /// </summary>
    private void WindowSlider_Loaded(object sender, RoutedEventArgs e)
    {
        //初始化滑块
        StartRect = new Rect(0, 0, 0, SliderHeight);
        EndRect = new Rect(SliderWidth, 0, SliderWidth, SliderHeight);
        ClipSlider();
    }

    private void LeftSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        ValueValidation();
        if (e.NewValue >= EndValue) // 检查值范围
            StartValue = EndValue; // 超出，重设为最大值
        ClipSlider();
    }

    private void RightSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        ValueValidation();
        if (e.NewValue <= StartValue)
            EndValue = StartValue;
        ClipSlider();
    }

    private void LeftInput_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        try
        {
            if (e.Key == Key.Enter) // 按回车时确认输入
                StartValue = Convert.ToInt32(((TextBox)sender).Text);
        }
        catch (Exception ex)
        {
            ((TextBox)sender).Text = StartValue.ToString();
        }
    }

    private void RightInput_KeyUp(object sender, KeyEventArgs e)
    {
        try
        {
            if (e.Key == Key.Enter)
                EndValue = Convert.ToInt32(((TextBox)sender).Text);
        }
        catch (Exception ex)
        {
            ((TextBox)sender).Text = EndValue.ToString();
        }
    }

    private void ValueValidation()
    {
        if (StartValue < Minimum)
        {
            StartValue = Minimum;
        }
        if (EndValue > Maximum)
        {
            EndValue = Maximum;
        }
    }
}
